package com.boyaa.shells.tool 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	/**
	 * 快捷购买界面的按钮
	 * 特点：同一类型的按钮，有且仅有一个按钮要被选中
	 * 
	 * @author renwentao
	 */
	public class QuickBuyButton
	{
		private var _btn:MovieClip;//按钮元件
		private var _btnList:Array;//同一类型按钮数组
		private var _fun:Function;//按钮点击时处理函数
		
		public function QuickBuyButton(btn:MovieClip, arr:Array, fun:Function) 
		{
			_btn = btn;
			_btn.stop();
			_btnList = arr;
			_fun = fun;
		}
		
		/**
		 * 添加事件
		 * */
		public function addEvent():void
		{
			_btn.mouseEnabled = true;
			_btn.buttonMode = true;
			_btn.addEventListener(MouseEvent.ROLL_OVER, rollOver);
			_btn.addEventListener(MouseEvent.ROLL_OUT , rollOut);
			_btn.addEventListener(MouseEvent.CLICK, onClick);
			
		}
		
		/**
		 * 删除事件
		 * */
		public function removeEvent():void
		{
			_btn.mouseEnabled = false;
			_btn.buttonMode = false;
			_btn.removeEventListener(MouseEvent.ROLL_OVER, rollOver);
			_btn.removeEventListener(MouseEvent.ROLL_OUT , rollOut);
			_btn.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		/**
		 * 点击事件
		 * */
		public function onClick(e:MouseEvent):void 
		{
			_btn.gotoAndStop(3);
			/**
			 * 排除被点击的按钮，让同类型的其它按钮处于非选中状态
			 * */
			for (var i:int = 0; i < _btnList.length; i++)
			{
				if (_btn == (_btnList[i] as MovieClip))
				continue;
				(_btnList[i] as MovieClip).gotoAndStop(1);
			}
			_fun();
		}
		
		private function rollOut(e:MouseEvent):void 
		{
			if (_btn.currentFrame == 3)
				return;
			_btn.gotoAndStop(1);
		}
		
		private function rollOver(e:MouseEvent):void 
		{
			if (_btn.currentFrame == 3)
				return;
			_btn.gotoAndStop(2);
		}
	}

}